package com.zjh.springcloud.controller;

import com.zjh.common.R;
import com.zjh.entity.Menu;
import com.zjh.entity.Order;
import com.zjh.entity.User;
import com.zjh.springcloud.feign.OrderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * ClassName: OrderClientController
 * Description:
 * @Author zjh
 * @Create 2023/10/30 13:36
 */
@Controller
@RequestMapping("/orderClient")
public class OrderClientController {
	
	@Autowired
	private OrderFeign orderFeign;
	
	@GetMapping("orderHandler")
	public String orderHandler(){
		return "order_handler";
	}
	
	@GetMapping("/orders")
	public String order(){
		return "order";
	}
	@GetMapping("/index")
	public  String index(){
		return "index";
	}
	@GetMapping("findAllByState")
	@ResponseBody
	public R<List<Order>> findAllByState(
			@RequestParam("page")Integer page,
			@RequestParam("limit")Integer limit){
		
		return orderFeign.findByState(0,(page-1)*limit,limit);
	}
	
	@GetMapping("updateState/{id}/{state}")
	public String updateState(HttpSession session,
	                          @PathVariable Integer id,
	                          @PathVariable Integer state){
		HashMap<String,Object> map = (HashMap<String,Object>)session.getAttribute("obj");
		Integer aid =(Integer) map.get("id");
		orderFeign.updateState(id,aid,state);
		return "redirect:/orderClient/orderHandler";
	}
	@GetMapping("findAllByUid")
	@ResponseBody
	public R<List<Order>> findAllByUid(HttpSession session,
	                            @RequestParam("page")Integer page,
	                            @RequestParam("limit")Integer limit){
		HashMap<String,Object> map = (HashMap<String,Object>)session.getAttribute("obj");
		Integer uid =(Integer) map.get("id");
		
		return orderFeign.findByUid(uid,(page-1)*limit,limit);
	}
	@GetMapping("save/{id}")
	public String save(@PathVariable Integer id, HttpSession session){
		HashMap<String, Object> map = (HashMap<String, Object>) session.getAttribute("obj");
		Integer uid = (Integer)map.get("id");
		User user=new User();
		user.setId(uid);
		Menu menu=new Menu();
		menu.setId(id);
		Order order = new Order(null, user, menu, null, new Date(), 0);
		orderFeign.save(order);
		return "order";
	}
}
